//
//  LGAddPatientRemarkController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/2.
//  添加患者备注

#import "LGAddPatientRemarkController.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
#define placeHolder  @"请输入"

@interface LGAddPatientRemarkController ()<UITextViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UITextView *textView;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;

@end

@implementation LGAddPatientRemarkController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    self.title = (_remark && _remark.length>0)?@"修改备注":@"添加备注";
    [self.view addSubview:self.baseView];
    [self.view addSubview:self.sureBtn];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.textView];
    [self.baseView addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.baseView).offset(-viewPix(16));
    }];
    NSString *remark = [self.remark isEmptyString]?@"":self.remark;
    self.numberLabel.text = [NSString stringWithFormat:@"%ld / 500",remark.length];
}

-(void)saveRemarkData{
    if(self.textView.text.length == 0 || [self.textView.text isEqualToString:placeHolder]){
        [TooltipView showMessage:@"请输入备注内容" offset:0];
        return;
    }
    [RequestUtil withPOST:@"/doctor/patient.HealthRecords/savebz" parameters:@{@"patient_id":self.patientid,@"content":self.textView.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [TooltipView showMessage:@"保存成功" offset:0];
            if (self.patientRemarkUpdate) {
                self.patientRemarkUpdate(self.textView.text);
            }
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}

-(void)setRemark:(NSString *)remark{
    _remark = remark;
    self.textView.text = remark;
    if(remark.length == 0 || [remark isEqualToString:placeHolder]){
        self.textView.text = placeHolder;
        self.textView.textColor = [UIColor colorWithString:@"#999999"];
    }else{
        self.textView.textColor = [UIColor colorWithString:@"#333333"];
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.textView.textColor = [UIColor colorWithString:@"#333333"];
    if([textView.text isEqualToString:placeHolder]){
        self.textView.text = @"";
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    NSString *content = textView.text;
    if(content.length == 0||[textView.text isEqualToString:placeHolder]){
        self.textView.text = placeHolder;
        self.textView.textColor = [UIColor colorWithString:@"#999999"];
    }else{
        self.textView.textColor = [UIColor colorWithString:@"#333333"];
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    NSString *lang = textView.textInputMode.primaryLanguage; // 键盘输入模式
    NSString *content = textView.text;
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) { // 没有高亮
            textView.text = (textView.text.length>500)?[textView.text substringToIndex:500]:textView.text;
            self.numberLabel.text = [NSString stringWithFormat:@"%ld / 500",textView.text.length];
        }
    }else{
        textView.text = (textView.text.length>500)?[textView.text substringToIndex:500]:textView.text;
        self.numberLabel.text = [NSString stringWithFormat:@"%ld / 500",textView.text.length];
    }
}


-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _baseView.frame = CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(327));
        _baseView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"备注" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(16), viewPix(16), viewPix(50), viewPix(22));
    }
    return _titleLabel;
}

-(UITextView *)textView{
    if(!_textView){
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(45), Screen_W-viewPix(64), self.baseView.bounds.size.height-viewPix(90))];
        _textView.tintColor = [UIColor colorWithString:@"#333333"];
        _textView.textColor = [UIColor colorWithString:@"#333333"];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.font = LGFont(14);
        _textView.delegate = self;
    }
    return _textView;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"0/500" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _numberLabel;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"确定" topMargin:kMaxY(self.baseView.frame)+viewPix(173)];
        [_sureBtn addTarget:self action:@selector(saveRemarkData) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
